package androidgames.networkgames.screens.components;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.impl.AndroidPixmap;

public class BackgroundAnimation {

	private Bitmap bitmap = null;
	private int current = 0;
	private ArrayList<Pixmap> list;
	private int time = 0;
	private boolean show = true;
	private int currentImg = 0;
	private boolean stop = false;

	/**
	 * Constructor
	 * 
	 * @Pixmap background call getBackground() to setup background.
	 */
	public BackgroundAnimation(Pixmap pixmap) {
		current = 0;
		list = new ArrayList<Pixmap>();
		bitmap = Bitmap.createBitmap(((AndroidPixmap) pixmap).bitmap);
	}

	/**
	 * Constructor
	 * 
	 * @list List background. call getBackground() to setup background.
	 */
	public BackgroundAnimation(ArrayList<Pixmap> list) {
		current = 0;
		currentImg = 0;
		this.list = new ArrayList<Pixmap>();
		this.list = list;
		bitmap = Bitmap
				.createBitmap(((AndroidPixmap) list.get(currentImg)).bitmap);
	}

	/** setup Background */
	public Pixmap getBackground() {
		Paint paint = new Paint();
		if (stop == false) {
			if (list == null) {
				drawOne(paint);
			} else {
				drawList(paint);
			}
		}
		if (stop == true) {
			if (current > 0)
				current -= 25;
			if (current == 0) {
			}
			paint.setAlpha(current);
		}
		return currentFrame(bitmap, paint);
	}

	/** draw bitmap if constructor one */
	private void drawOne(Paint paint) {
		if (current < 250) {
			current += 25;
			paint.setAlpha(current);
		}
	}

	/** draw list background if constructor two */
	private void drawList(Paint paint) {
		if (current <= 250 && show) {
			current += 25;
			paint.setAlpha(current);
			if (current == 250 && list.size() > 1) {
				show = false;
				time = 0;
			}
		}
		if (!show) {
			if (time < 20)
				time++;
			if (time == 20) {
				if (current > 0)
					current -= 25;
				paint.setAlpha(current);
				if (current == 0 && currentImg < list.size()) {
					currentImg++;
					if (currentImg == list.size())
						currentImg = 0;
					bitmap = Bitmap.createBitmap(((AndroidPixmap) list
							.get(currentImg)).bitmap);
					current = 0;
					show = true;
				}
			}
		}
	}

	/** Stop background animation after show off the current background */
	public boolean stopBG() {
		stop = true;
		return true;
	}

	/** resume background animation */
	public boolean startBG() {
		stop = false;
		return true;
	}

	/**get the current pixmap*/
	private Pixmap currentFrame(Bitmap bitmap, Paint paint) {
		Pixmap pixmap = new AndroidPixmap(draw(paint, bitmap),
				PixmapFormat.RGB565);
		return pixmap;
	}

	/**create buffer bitmap*/
	private Bitmap draw(Paint paint, Bitmap bitmap) {
		Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
				Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bm);
		canvas.drawBitmap(bitmap, 0, 0, paint);
		return bm;
	}

}
